import categoryApi from '@/api/shop/category'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'
export default {
  directives: { permission },
  data() {
    return {
      addFormVisible: false,
      addFormTitle: '新增热门分类',
      updateFormVisible: false,
      updateFormTitle: '修改排序',
      addForm: {
        name: '',
        id: '',
        secondId: 0,
      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      apiUrl: getApiUrl(),
      banner: {
        idCategory: '',
        activeName: 'first',
        visible: false,
        list: []
      },
      allFirstClassList: [],
      allSecondClassList: [],
      allSecondClassMap: {},
      allHotClassList: [],
      rowSeleted: {},
      updateForm: {},
      flag: false,
      currentFirst: "",
    }

  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData()
      this.getAllFirstClass()
    },
    fetchData() {
      this.listLoading = true
      this.allHotClassList = []
      categoryApi.getAllHotCategory().then(response => {
        let tmpList = response.data
        for (let index = 0; index < tmpList.length; index++) {
          const e = tmpList[index];
          let t = {
            "id": e.id,
            "first": e.categoryFirst.name,
            "second": e.name,
            "icon": getApiUrl() + '/file/getImgStream?idFile=' + e.icon,
            "hotSort": e.hotSort,
          }
          this.allHotClassList.push(t)
        }
        this.listLoading = false
      })
    },
    getAllFirstClass() {
      categoryApi.getFirstCategoryAndSecond().then(response => {
        //console.log("response", response);
        let resp = response.data
        for (let i = 0; i < resp.length; i++) {
          const first = resp[i];
          //console.log("first", first);
          this.allSecondClassMap[first.id] = []
          for (let j = 0; j < first.categoryList.length; j++) {
            const second = first.categoryList[j];
            //console.log("second", second);
            let secondInfo = {
              "id": second.id,
              "name": second.name,
            }
            this.allSecondClassMap[first.id].push(secondInfo)
          }
          let firstInfo = {
            "id": first.id,
            "name": first.name,
          }
          this.allFirstClassList.push(firstInfo)
        }

      })
    },
    firstClassChange() {
      this.allSecondClassList = this.allSecondClassMap[this.addForm.id]
    },
    add() {
      this.resetForm()
      this.addFormTitle = '添加商品类别'
      this.addFormVisible = true
      this.isAdd = true
    },
    resetForm() {
      this.addForm = {
        name: '',
        url: '',
        icon: '',
        id: ''
      }
    },
    save() {
      if (!this.addForm.id) {
        this.$message({
          message: "请选择一级分类",
          type: 'warning'
        })
        return
      }
      if (!this.addForm.secondId) {
        this.$message({
          message: "请选择二级分类",
          type: 'warning'
        })
        return
      }
      let param = {
        "id": this.addForm.secondId,
        "isHot": 1
      }
      categoryApi.updateSecondClassIsHot(param).then(response => {
        this.$message({
          message: this.$t('common.optionSuccess'),
          type: 'success'
        })
        this.fetchData()
      })
      this.addFormVisible = false
    },
    update() {
      //console.log(123);
      if (this.checkSel()) {
        //console.log(321);
        this.updateFormVisible = true
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.rowSeleted.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          let param = {
            "id": id,
            "isHot": 0
          }
          categoryApi.updateSecondClassIsHot(param).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          })
        }).catch(() => {
        })
      }
    },
    checkSel() {
      //console.log(3, this.rowSeleted)
      if (this.rowSeleted && this.rowSeleted.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      //console.log(4, currentRow)
      if (this.rowSeleted && currentRow) {
        this.rowSeleted.id = currentRow.id
      }
    },
    clickRow(row) {
      this.rowSeleted.id = row.id
      this.updateForm = {
        "first": row.first,
        "second": row.second,
        "hotSort": row.hotSort,
      }
    },
    updateHotSort() {
      let updateHotSortParam = {
        "id": this.rowSeleted.id,
        "sort": this.updateForm.hotSort,
      }
      categoryApi.updateHotSort(updateHotSortParam).then(response => {
        this.$message({
          message: this.$t('common.optionSuccess'),
          type: 'success'
        })
        this.updateFormVisible = false
        this.fetchData()
      })
    },

  }
}
